package com.jiulan.hearthearing.ui.course.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseActivity;
import com.jiulan.hearthearing.constants.UrlConstants;
import com.jiulan.hearthearing.http.HttpRequest;
import com.jiulan.hearthearing.http.HttpStringCallBack;
import com.jiulan.hearthearing.ui.course.model.CourseDetailsModel;
import com.shehuan.niv.NiceImageView;

import java.util.HashMap;

/**
 * 文章详情
 */
public class CourseDetailsArticleActivity extends BaseActivity implements View.OnClickListener {
    private int id;
    private CourseDetailsModel model;

    private ImageView _barBack;
    private TextView _barTitle;
    private LinearLayout llAction;
    private ImageView ivCollect;
    private ImageView ivShare;
    private NiceImageView nivPic;
    private TextView tvTitle;
    private TextView tvAuthor;
    private TextView tvTime;
    private TextView tvCheckNum;
    private TextView tvBuyPrice;
    private TextView tvJianjie;
    private TextView tvContent;
    private TextView tvPrice;
    private TextView tvYuanjia;
    private TextView tvAddCart;
    private TextView tvBaoming;

    public static void actionStart(Context context, int id) {
        Intent intent = new Intent(context, CourseDetailsArticleActivity.class);
        intent.putExtra("id", id);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_course_details_article;
    }

    @Override
    protected void initView() {
        id = getIntent().getIntExtra("id", 0);
        _barBack = findViewById(R.id._barBack);
        _barTitle = findViewById(R.id._barTitle);
        llAction = findViewById(R.id.llAction);
        ivCollect = findViewById(R.id.ivCollect);
        ivShare = findViewById(R.id.ivShare);
        nivPic = findViewById(R.id.nivPic);
        tvTitle = findViewById(R.id.tvTitle);
        tvAuthor = findViewById(R.id.tvAuthor);
        tvTime = findViewById(R.id.tvTime);
        tvCheckNum = findViewById(R.id.tvCheckNum);
        tvBuyPrice = findViewById(R.id.tvBuyPrice);
        tvJianjie = findViewById(R.id.tvJianjie);
        tvContent = findViewById(R.id.tvContent);
        tvPrice = findViewById(R.id.tvPrice);
        tvYuanjia = findViewById(R.id.tvYuanjia);
        tvAddCart = findViewById(R.id.tvAddCart);
        tvBaoming = findViewById(R.id.tvBaoming);
        _barBack.setOnClickListener(this);
        ivCollect.setOnClickListener(this);
        ivShare.setOnClickListener(this);
        tvAddCart.setOnClickListener(this);
        tvBaoming.setOnClickListener(this);
        _barTitle.setText("文章详情");
        llAction.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        /**
         * 课程详情
         */
        new HttpRequest(getContext()).doGet(UrlConstants.course_details + String.valueOf(id), "", new HashMap(), CourseDetailsModel.class, new HttpStringCallBack() {
            @Override
            public void onSuccess(Object result) {
                if (result instanceof CourseDetailsModel) {
                    model = (CourseDetailsModel) result;
                    setView();
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }

    private void setView() {
        Glide.with(getContext()).load(model.getData().getLecturer_info().getPic()).into(nivPic);
        tvTitle.setText(model.getData().getCourse().getTitle());
        tvAuthor.setText(model.getData().getLecturer_info().getName() + "·" + model.getData().getLecturer_info().getTitle());
//        tvTime.setText(model.getData().getCourse().getAdd_time());
        tvCheckNum.setText(String.valueOf(model.getData().getCourse().getBuy_num() + "人已读"));
        tvBuyPrice.setText(String.valueOf(model.getData().getCourse().getPrice()));
        tvJianjie.setText(String.valueOf(model.getData().getCourse().getShort_title()));
        tvContent.setText(String.valueOf(model.getData().getCourse().getShort_content()));
        tvPrice.setText("券后" + String.valueOf(model.getData().getCourse().getPrice()) + "学习币");
        tvYuanjia.setText("原价" + String.valueOf(model.getData().getCourse().getDel_price()) + "学习币");
        tvYuanjia.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
    }

    @Override
    protected int getTitleBarType() {
        return TITLEBAR_NULL;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id._barBack:
                finish();
                break;
            case R.id.ivCollect:
                break;
            case R.id.ivShare:
                break;
        }
    }
}
